<?php
namespace Company\Controller;
use Think\Controller;
class ProductController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
		C('erp_dbname',$g['db']);
        $db_name=$g['db'];
		//数据库连接基础配置-begin
		C('db_config.DB_TYPE',C('DB_TYPE'));
		C('db_config.DB_HOST',C('DB_HOST'));
		C('db_config.DB_USER',C('DB_USER'));
		C('db_config.DB_PWD',C('DB_PWD'));
		C('db_config.DB_PORT',C('DB_PORT'));
		C('db_config.DB_CHARSET',C('DB_CHARSET'));
		C('db_config.DB_NAME',C('erp_dbname'));
		//数据库连接基础配置-end
        $this->product = M('product',C('DB_PREFIX_T'),C('db_config'));
    }
	public function index_hasexamined(){
		$this->index(2);
		
	}
    public function index($is_examined=0){
        if(!isset($_GET['p']) || $_GET['p']==''){
            $p=1;
        }else{
            $p=intval($_GET['p']);
        }
		if($is_examined==2){
			$where['examined']=2;
		}else{
			$where['examined']=array('neq',2);
		}
        if(I("get.start_time")!='') {
            $where['created_at']= array('gt',I("get.start_time"));
        }
        if(I("get.end_time")!=''){
            $where['created_at'] = array('lt',I("get.end_time"));
        }
        if(I("get.start_time")!='' && I("get.end_time")!=''){
            $where['created_at']= array(array('gt',I("get.start_time")),array('lt',I("get.end_time")));
        }
        if(I("get.name")!=''){
            $where['name']=array('like',"%".I("get.name")."%");
        }
		if(I("get.sn")!=''){
            $where['sn']=array('like',"%".I("get.sn")."%");
        }

        if(I("get.page_count")=='' || I("get.page_count")<=0){
            $page_count=15;
        }else{
            $page_count=intval(I("get.page_count"));
        }
        if(I("get.sort")!=''){
            $order=I("get.sort")." ".I("get.dec");
        }else{
            $order='id desc';
        }
        if(!isset($where)){
            $where="1=1";
        }
		$excel=I('excel');
		
        $list = $this->product->where($where)->order($order)->page($p.','.$page_count)->select();
        $this->assign('list',$list);

		if($excel){
			$list = $this->product->where($where)->order($order)->select();
			foreach($list as $k=>$v){
					$list[$k]['goods_no']=" ".$v['goods_no'];
					$list[$k]['sn']=" ".$v['sn'];
					$list[$k]['hs_code']=" ".$v['hs_code'];
					$list[$k]['pn']=" ".$v['pn'];
					$list[$k]['zccode']=" ".$v['zccode'];
					$list[$k]['net_weight']=sprintf("%.2f",$v['net_weight']);
					$list[$k]['gross_weight']=sprintf("%.2f",$v['gross_weight']);
			}
			$xlsName  = "产品列表";
			$xlsCell  = array(
				array('name','商品名称'),
				array('goods_no','商品编码'),
				array('price','价格（单价）'),
				array('unit','第一单位'),
				array('sn','条形码'),
				array('hs_code','hs_code'),
				array('pn','料号'),
				array('standard','规格'),
				array('currency','币制'),
				array('country','产销国'),
				array('zccode','账册编码'),
				array('created_at','添加时间'),
				array('item_type','料件性质'),
				array('item_no','项号'),
				array('net_weight','净重'),
				array('gross_weight','毛重'),
				array('goods_tax','税率')
			);
			$Aexcel=A('Order');
			$Aexcel->exportExcel($xlsName,$xlsCell,$list);
			die;
		}
        $count      = $this->product->where($where)->count();
        $this->assign('count',$count);
        $Page       = new \Think\Page($count,$page_count);
        $show       = $Page->show();
		
		$owner_status=session('company.owner_status');
		// dump($owner_status);
		// die;
		$this->assign('owner_status',$owner_status);
        $this->assign('page',$show);
        $this->display('index');
    }
    public function add(){
        $this->assign('time',date("YmdHis",time()).rand(100,999));
        $this->display();
    }
	public function add_two(){
        $unit=M('Unit');
		$country=M('Country');
		$coin=M('Coin');
		$book = M('Book',C('DB_PREFIX_T'),C('db_config'));
		
		$book_list=$book->where(array('examined'=>2))->select();
		$unit_list=$unit->select();
		$coin_list=$coin->select();
		$country_list=$country->select();
		
		$this->assign('coin_list',$coin_list);
		$this->assign('book_list',$book_list);
		$this->assign('unit_list',$unit_list);
		$this->assign('country_list',$country_list);
        $this->display();
    }
	//料号校检
	// public function get_sourceno_info($pn){
		// $res=$this->get_sorce_info($pn);
		// if($res['status']==1){
			// alert(1,'通过');
		// }
	// }
	//料号校检
	public function get_sourceno_info($pn){
		if(empty($pn)){
			alert(0,'料号不能为空');
		}
		$url=C('base_config.source_info');
		if(empty($url)){
			alert(0,'没有配置接口地址');
		}
		$data['data']['source_no']=$pn;
		$data['customer']['user_code']=session('company.user_code');
		$data['customer']['user_key']=session('company.user_key');
		$push_res=send_curl($url,$data);
		$push_res=json_decode($push_res,true);
		// dump($push_res);
		if(!$push_res['flag']){
			alert(0,'没有此料号');
		}else{
			alert(1,$push_res['data']);
		}
	}
	//添加产品（货主/跨境）
	public function confirm_add_two(){
		$param=I('param.','','trim');
		$m = M('Product',C('DB_PREFIX_T'),C('db_config'));
		if(empty($param['name'])){
			alert(0,'请填写产品名称');
			exit;
		}
		if(empty($param['sn'])){
			alert(0,'请填写产品名称');
			exit;
		}
		if(empty($param['zccode'])){
			alert(0,'请选择账册编码');
			exit;
		}
		if(empty($param['unit'])){
			alert(0,'请选择销售单位');
			exit;
		}
		if(empty($param['standard'])){
			alert(0,'请填写规格');
			exit;
		}
		if(empty($param['sell_price'])){
			alert(0,'请填写销售价');
			exit;
		}
		if(empty($param['purchase_price'])){
			$param['purchase_price']=0;
			// alert(0,'请填写进价');
			// exit;
		}
		if(empty($param['country'])){
			alert(0,'请选择产销国');
			exit;
		}
		if(empty($param['trade_curr'])){
			alert(0,'请填写币制');
			exit;
		}
		if(empty($param['net_weight'])){
			alert(0,'请填写净重');
			exit;
		}if(empty($param['gross_weight'])){
			alert(0,'请填写毛重');
			exit;
		}
		$data=array();
		$data['category']='无';
		$data['goods_cate']=1;
		
		$data['name']=$param['name'];
		$data['sn']=$param['sn'];
		$data['zccode']=$param['zccode'];
		$has_c=$m->where(array('sn'=>$data['sn'],'zccode'=>$data['zccode']))->count();
		if($has_c>0){
			alert(0,'该条码已经存在');
		}
		$data['unit_no']=$param['unit'];
		$data['unit_no_t']=$param['unit_t'];
		
		$data['unit']=M('Unit')->where(array('unit_no'=>$param['unit']))->getField('unit_name');
		$data['unit_t']=M('Unit')->where(array('unit_no'=>$param['unit_t']))->getField('unit_name');
		
		$data['standard']=$param['standard'];
		$data['sell_price']=$param['sell_price'];
		$data['purchase_price']=$param['purchase_price'];
		$data['net_weight']=$param['net_weight'];
		$data['gross_weight']=$param['gross_weight'];
		$data['coin_code']=$param['trade_curr'];
		
		$book= M('book',C('DB_PREFIX_T'), C('db_config'));
		$book_info=$book->where(array('book_no'=>$param['zccode']))->find();
		$merchant_code=$book_info['merchant_code'];
		
		$sn=$param['sn'];
		$sn_length=strlen($sn);
		// if($sn_length<20){
			// $sn_new=$sn;
			// for($i=0;$i<20-$sn_length;$i++){
				// $sn_new=$sn_new.'0';
			// }
		// }else{
			// $sn_new=substr($sn,-20,20);
		// }
		$sn_new=$sn;
		$goods_no=$merchant_code.$sn_new;
        $data['goods_no']=$goods_no;//商家编码（10位）+ 条形码（不足20位数字的在后面补0，超过20位的，截取后面的20位字符）
		//商家编码定义：公司--C+海关十位编码或者组织机构代码。如果有海关十位编码的优先考虑海关十位编码，没有海关十位编码的再考虑组织机构代码
        //注：组织机构代码不足十位的在后边补0
        //个人--P+手机号码。截取手机号码后十位

		$data['pn']=$param['sn'];
		$data['examined']=1;//推送状态
		$data['country_code']=$param['country'];
		$data['currency']=M('Coin')->where(array("coin_code"=>$param['trade_curr']))->getField('coin_name');
        $data['country']=M('Country')->where(array('country_code'=>$param['country']))->getField('country_name');
		
		// $api_url="http://tzoms.com/index.php/Api/Goods/goodsIn";//url接口地址
		$api_url="http://www.t-cang.com/oms/index.php/Api/Goods/goodsIn";//url接口地址
		$u=M('user');
		$username=session('company.username');

		$u_info=$u->where(array('username'=>$username))->find();
		
		$push_data=array();
		$push_data['data']['goods_name']=$data['name'];
		$push_data['data']['goods_sn']=$data['goods_no'];
		$push_data['data']['source_no']=$data['pn'];
		$push_data['data']['goods_spec']=$data['standard'];
		$push_data['data']['purchase_price']=$data['purchase_price'];
		$push_data['data']['price']=$data['sell_price'];
		$push_data['data']['net_weight']=$data['net_weight'];
		$push_data['data']['gross_weight']=$data['gross_weight'];
		$push_data['data']['trade_curr']=$data['coin_code'];
		$push_data['data']['origin_country']=$data['country_code'];
		$push_data['data']['type']=1;//1:新增2：修改
		$push_data['customer']['user_code']=$u_info['user_code'];
		$push_data['customer']['user_key']=$u_info['user_key'];
		
		$res=push($api_url,$push_data);

		$res=json_decode($res,true);
		if(!$res['flag']){
			alert(0,$res['tip']);
		}else{
			
			$add_id=$m->add($data);
			if(!$add_id){
				alert(0,'添加失败');
			}else{
				alert(1,'添加成功！');
			}
		}
	}
	//编辑产品（货主/跨境）
	public function confirm_edit_two(){
		$param=I('param.','','trim');
		$id=$param['id'];
		$m = M('Product',C('DB_PREFIX_T'),C('db_config'));
		if(empty($param['name'])){
			alert(0,'请填写产品名称');
			exit;
		}
		if(empty($param['sn'])){
			alert(0,'请填写产品名称');
			exit;
		}
		if(empty($param['zccode'])){
			alert(0,'请选择账册编码');
			exit;
		}
		if(empty($param['unit'])){
			alert(0,'请选择销售单位');
			exit;
		}
		if(empty($param['standard'])){
			alert(0,'请填写规格');
			exit;
		}
		if(empty($param['sell_price'])){
			alert(0,'请填写销售价');
			exit;
		}
		if(empty($param['purchase_price'])){
			$param['purchase_price']=0;
			// alert(0,'请填写进价');
			// exit;
		}
		if(empty($param['country'])){
			alert(0,'请选择产销国');
			exit;
		}
		if(empty($param['trade_curr'])){
			alert(0,'请填写币制');
			exit;
		}
		if(empty($param['net_weight'])){
			alert(0,'请填写净重');
			exit;
		}
		if(empty($param['gross_weight'])){
			alert(0,'请填写毛重');
			exit;
		}
		$data=array();

		$data['name']=$param['name'];
		$data['sn']=$param['sn'];
		$data['zccode']=$param['zccode'];
		$has_c=$m->where(array('id'=>array('neq',$id),'sn'=>$data['sn'],'zccode'=>$data['zccode']))->count;
		if($has_c>0){
			alert(0,'此条形码已经存在');
		}
		$data['unit_no']=$param['unit'];
		$data['unit_no_t']=$param['unit_t'];
		
		$data['unit']=M('Unit')->where(array('unit_no'=>$param['unit']))->getField('unit_name');
		$data['unit_t']=M('Unit')->where(array('unit_no'=>$param['unit_t']))->getField('unit_name');
		
		$data['standard']=$param['standard'];
		$data['sell_price']=$param['sell_price'];
		$data['purchase_price']=$param['purchase_price'];
		$data['net_weight']=$param['net_weight'];
		$data['gross_weight']=$param['gross_weight'];
		$data['coin_code']=$param['trade_curr'];
		
		$book= M('book',C('DB_PREFIX_T'), C('db_config'));
		$book_info=$book->where(array('book_no'=>$param['zccode']))->find();
		$merchant_code=$book_info['merchant_code'];
		
		$sn=$param['sn'];
		$sn_length=strlen($sn);
		// if($sn_length<20){
			// $sn_new=$sn;
			// for($i=0;$i<20-$sn_length;$i++){
				// $sn_new=$sn_new.'0';
			// }
		// }else{
			// $sn_new=substr($sn,-20,20);
		// }
		$sn_new=$sn;
		$goods_no=$merchant_code.$sn_new;
        $data['goods_no']=$goods_no;//商家编码（10位）+ 条形码（不足20位数字的在后面补0，超过20位的，截取后面的20位字符）
		//商家编码定义：公司--C+海关十位编码或者组织机构代码。如果有海关十位编码的优先考虑海关十位编码，没有海关十位编码的再考虑组织机构代码
        //注：组织机构代码不足十位的在后边补0
        //个人--P+手机号码。截取手机号码后十位

		$data['pn']=$param['sn'];
		$data['examined']=1;//推送状态
		$data['country_code']=$param['country'];
		$data['currency']=M('Coin')->where(array("coin_code"=>$param['trade_curr']))->getField('coin_name');
        $data['country']=M('Country')->where(array('country_code'=>$param['country']))->getField('country_name');
		
		// $api_url="http://tzoms.com/index.php/Api/Goods/goodsIn";//url接口地址
		$api_url="http://www.t-cang.com/oms/index.php/Api/Goods/goodsIn";//url接口地址
		
		$u=M('user');
		$username=session('company.username');

		$u_info=$u->where(array('username'=>$username))->find();
		
		$push_data=array();
		$push_data['data']['goods_name']=$data['name'];
		$push_data['data']['goods_sn']=$data['goods_no'];
		$push_data['data']['source_no']=$data['pn'];
		$push_data['data']['goods_spec']=$data['standard'];
		$push_data['data']['purchase_price']=$data['purchase_price'];
		$push_data['data']['price']=$data['sell_price'];
		$push_data['data']['net_weight']=$data['net_weight'];
		$push_data['data']['gross_weight']=$data['gross_weight'];
		$push_data['data']['trade_curr']=$data['coin_code'];
		$push_data['data']['origin_country']=$data['country_code'];
		$receive_time=$m->where(array('id'=>$id))->getField("receive_time");
		if($receive_time==0){
			$push_data['data']['type']=1;//1:新增2：修改
		}else{
			$push_data['data']['type']=2;//1:新增2：修改
		}
		$push_data['customer']['user_code']=$u_info['user_code'];
		$push_data['customer']['user_key']=$u_info['user_key'];
		
		$res=push($api_url,$push_data);

		$res=json_decode($res,true);
		if(!$res['flag']){
			alert(0,$res['tip']);
		}else{
			
			$res_save=$m->where(array('id'=>$id))->save($data);
			if($res_save===false){
				alert(0,'修改失败');
			}else{
				alert(1,'修改成功！');
			}
		}
	}
	// 料号、商品规格、币制、产销国
    public function addD(){
		$param=I('param.','','trim');
		$pro_status=$param['pro_status'];
		if(empty($pro_status)){
			alert(0,'请选择账册编码');
			exit;
		}
		$hs_code=$param['hs_code'];
		$item_no=$param['item_no'];
		$name=$param['name'];
		$goods_tax=$param['goods_tax'];
		$zccode=$param['zccode'];
		$price=$param['price'];
		$sell_price=$param['sell_price'];
		$net_weight=$param['net_weight'];
		$gross_weight=$param['gross_weight'];
		$unit=$param['unit'];
		$category=$param['category'];
		$country=$param['country'];
		$standard=$param['standard'];
		$currency=$param['currency'];
		$unit_no=$param['unit_no'];
		$unit_t=$param['unit_t'];
		$item_type=$param['item_type'];
		$unit_no_t=$param['unit_no_t'];
		$unit_t_goods_spec=$param['unit_t_goods_spec'];
		$sn=$param['sn'];//条形码
		$pn=$param['pn'];//料号
		$goods_cate=$param['goods_cate'];
		$unit_t_goods_spec=$param['unit_t_goods_spec'];
		
		
		if($pro_status==2){//保税/直邮
			if(empty($hs_code)){
				alert(0,'HSCODE不能为空');
			}
			if(empty($country)){
				alert(0,'产销国不能为空');
			}
			
			if($item_type==""){
				alert(0,'料件性质不能为空');
			}
			if(empty($item_no)){
				alert(0,'项号不能为空');
			}
			if(empty($goods_tax)){
				alert(0,'税率不能为空');
			}
			if(empty($net_weight)){
				alert(0,'净重不能为空');
			}
			if(empty($gross_weight)){
				alert(0,'毛重不能为空');
			}
		}
		if(empty($price)){
			alert(0,'第一单位价格不能为空或0');
		}
		if($sn==''){
			alert(0,'条形码不能为空');
		}
		if($name==''){
			alert(0,'产品名称不能为空');
		}
		if($zccode==''){
			alert(0,'账册编码不能为空');
		}
		
		if($unit==''){
			alert(0,'第一单位不能为空');
		}
		if($category==''){
			alert(0,'类目不能为空');
		}
		if($country=='' || $standard=='' || $currency==''){
			alert(0,'产销国/规格/币制都不能为空');
		}

		$g=session('company');
        $db_name=$g['db'];
        $this->book = M('book',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		
		$country_info=explode('-',$country);
		$coin_info=explode('-',$currency);
		
        $data['name']=$name;
        $data['zccode']=$zccode;
		
		$book_info=$this->book->where(array('book_no'=>$zccode))->find();
		$merchant_code=$book_info['merchant_code'];
        $data['price']=$price;
		$data['sell_price']=$sell_price;
        $data['unit']=$unit;
        $data['unit_no']=$unit_no;
        // $data['price_t']=$price_t;
        $data['unit_t']=$unit_t;
		$data['item_no']=$item_no;
		$data['item_type']=$item_type;
		$data['hs_code']=$hs_code;
		$data['goods_tax']=$goods_tax;
        $data['unit_no_t']=$unit_no_t;
        $data['category']=$category;
        $massage=session('company');
        $data['operator']=$massage['username'];
		$data['sn']=$sn;
		$sn_length=strlen($sn);
		// if($sn_length<20){
			// $sn_new=$sn;
			// for($i=0;$i<20-$sn_length;$i++){
				// $sn_new=$sn_new.'0';
			// }
		// }else{
			// $sn_new=substr($sn,-20,20);
		// }
		$sn_new=$sn;
		$goods_no=$merchant_code.$sn_new;
        $data['goods_no']=$goods_no;//商家编码（10位）+ 条形码（不足20位数字的在后面补0，超过20位的，截取后面的20位字符）
		//商家编码定义：公司--C+海关十位编码或者组织机构代码。如果有海关十位编码的优先考虑海关十位编码，没有海关十位编码的再考虑组织机构代码
        //注：组织机构代码不足十位的在后边补0
        //个人--P+手机号码。截取手机号码后十位
		$data['country_code']=$country_info[0];
        $data['country']=$country_info[1];
        $data['standard']=$standard;
		$data['net_weight']=$net_weight;
		$data['gross_weight']=$gross_weight;
		$data['unit_t_goods_spec']=$unit_t_goods_spec;
		$data['pn']=$pn;
		$data['price']=$price;

		$data['coin_code']=$coin_info[0];
        $data['currency']=$coin_info[1];
		$data['goods_cate']=$goods_cate;
		
		if($this->product->where(array('goods_no'=>$goods_no))->count()>0){
            alert(0,'条形码不能重复');
        }
        if($this->product->where(array('name'=>$name))->count()>0){
            alert(0,'产品名称不能重复');
        }
        $result=$this->product->add($data);
		echo $this->product->getlastsql();
		die;
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
    public function push($id,$warehouse_id,$twice=0){
        $g=session('company');
        $db_name=$g['db'];
        $user_id=$g['company_id'];
        $f=C('DB_PREFIX_T');
        $this->p = M('product',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $list=$this->p->where("id='".$id."'")->find();
        $list['erp_code']=$g['company_id'];
        //////如果已经推送则不能再次推送
		if($twice==0){//非二次推送 1：是二次推送
			if($list['examined']>0){
				alert(0,'请不要重复推送');
			}
		}
        
        unset($list['examined']);
        $g_id=M("user_warehouse")->where(array('user_id'=>$user_id))->getField('warehouse_group_id');
        $group_id=explode(',',$g_id);
// dump($warehouse_id);
// dump($group_id);
// dump(in_array($warehouse_id,$group_id));
// die;
		if(!in_array($warehouse_id,$group_id)){
			alert(0,'操作有误');
		}

        $d=M("warehouse")->join("__INTERFACES__ on __INTERFACES__.warehouse_id=__WAREHOUSE__.id")->where(C('DB_PREFIX')."warehouse.id='".$warehouse_id."'")->getField('vars,url,warehouse_code');
        $url=$d['var_4']['url'];
        $data=array(
			'twice'=>$twice,
            'product_id'=>$id,
            'erp_code'=>$g['company_id'],
            'warehouse_code'=>$d['var_4']['warehouse_code'],
            'list'=>$list,
        );
        $true=push($url,$data);//推送过去

        $true=json_decode($true,'true');
        if($true['status']==1){
			$data_s['examined']=1;
			$data_s['warehouse_code']=$d['var_4']['warehouse_code'];
			$res_s=$this->product->where("id='".$id."'")->save($data_s);
            if($res_s!==false){
                alert(1,'推送成功');
            }else{
                alert(0,'推送成功，状态更新失败');
            }
        }elseif($true['status']==2){
            alert(0,'对应的账册信息还没有推送');
        }else{
            alert(0,'推送失败');
        }
    }
    public function getWarehouse(){
        $g=session('company');
        $user_id=$g['company_id'];
        $group_id=M("user_warehouse")->where(array('user_id'=>$user_id))->getField('warehouse_group_id');
        $group_id=explode(',',$group_id);
        $where['id']=array('in',$group_id);
        $result=M("warehouse")->where($where)->getField('id,name',true);
        if($result){
            alert(1,$result);
        }else{
            alert(0,'暂无仓库信息');
        }
    }
    public function getBook(){
        $g=session('company');
        $user_id=$g['company_id'];
        $where['examined']=2;
        $result=M('book',C("DB_PREFIX_T"), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $g['db'])->field('book_no,type')->where($where)->select();
        if($result){
            alert(1,$result);
        }else{
            alert(0,'暂无账册信息');
        }
    }
    public function edit($id){
        if($id==''){
            echo "错误";
        }
        $this->result=$this->product->find($id);
        $this->display();
    }
	public function editD($id){
		$param=I('param.','','trim');
		$pro_status=$param['pro_status'];
		if(empty($pro_status)){
			alert(0,'请选择账册编码');
			exit;
		}
		
		$hs_code=$param['hs_code'];
		$item_no=$param['item_no'];
		$goods_no=$param['goods_no'];
		
		$name=$param['name'];
		$goods_tax=$param['goods_tax'];
		$zccode=$param['zccode'];
		$price=$param['price'];
		$sell_price=$param['sell_price'];
		$net_weight=$param['net_weight'];
		$gross_weight=$param['gross_weight'];
		$unit=$param['unit'];
		$category=$param['category'];
		$country=$param['country'];
		$standard=$param['standard'];
		$currency=$param['currency'];
		$unit_no=$param['unit_no'];
		$unit_t=$param['unit_t'];
		$item_type=$param['item_type'];
		$unit_no_t=$param['unit_no_t'];
		$unit_t_goods_spec=$param['unit_t_goods_spec'];
		$sn=$param['sn'];//条形码
		$pn=$param['pn'];//料号
		$goods_cate=$param['goods_cate'];
		$unit_t_goods_spec=$param['unit_t_goods_spec'];
		if($pro_status==2){//保税/直邮
			if(empty($hs_code)){
				alert(0,'HSCODE不能为空');
			}
			if(empty($country)){
				alert(0,'产销国不能为空');
			}
			if(empty($pn)){
				alert(0,'料号不能为空');
			}
			if($item_type==""){
				alert(0,'料件性质不能为空');
			}
			if(empty($item_no)){
				alert(0,'项号不能为空');
			}
			if(empty($goods_tax)){
				alert(0,'税率不能为空');
			}
			if(empty($net_weight)){
				alert(0,'净重不能为空');
			}
			if(empty($gross_weight)){
				alert(0,'毛重不能为空');
			}
		}
		if($sn==''){
			alert(0,'条形码不能为空');
		}
		if($name==''){
			alert(0,'产品名称不能为空');
		}
		if($zccode==''){
			alert(0,'账册编码不能为空');
		}
		if(empty($price)){
			alert(0,'第一单位价格不能为空或0');
		}
		if($unit==''){
			alert(0,'第一单位不能为空');
		}
		if($category==''){
			alert(0,'类目不能为空');
		}
		if($country=='' || $standard=='' || $currency==''){
			alert(0,'产销国/规格/币制都不能为空');
		}
		
        $where['goods_no']=$goods_no;
        $where['id']=array('neq',$id);
        if($this->product->where($where)->count()>0){
            alert(0,'商品编码不能重复');
        }
		
		$g=session('company');
        $db_name=$g['db'];
        $this->book = M('book',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		
		$country_info=explode('-',$country);
		$coin_info=explode('-',$currency);
        $data['name']=$name;
        $data['zccode']=$zccode;

		$book_info=$this->book->where(array('book_no'=>$zccode))->find();
		$merchant_code=$book_info['merchant_code'];
		
        $data['unit']=$unit;
		$data['item_no']=$item_no;
		$data['item_type']=$item_type;
        $data['unit_no']=$unit_no;
        $data['unit_t']=$unit_t;
        $data['unit_no_t']=$unit_no_t;
        $data['category']=$category;
		$data['hs_code']=$hs_code;
		$data['price']=$price;
		$data['sell_price']=$sell_price;
		$data['sn']=$sn;
		$data['goods_tax']=$goods_tax;
		$sn_length=strlen($sn);

		if($sn_length<20){
			$sn_new=$sn;
			for($i=0;$i<(20-$sn_length);$i++){
				$sn_new=$sn_new.'0';
			}
		}else{
			$sn_new=substr($sn,-20,20);
		}
		// $goods_no=$merchant_code.$sn_new;
        // $data['goods_no']=$goods_no;//商家编码（10位）+ 条形码（不足20位数字的在后面补0，超过20位的，截取后面的20位字符）
		//商家编码定义：公司--C+海关十位编码或者组织机构代码。如果有海关十位编码的优先考虑海关十位编码，没有海关十位编码的再考虑组织机构代码
        //注：组织机构代码不足十位的在后边补0
        //个人--P+手机号码。截取手机号码后十位
		$data['goods_no']=$goods_no;
		$data['country_code']=$country_info[0];
        $data['country']=$country_info[1];
        $data['standard']=$standard;
		$data['unit_t_goods_spec']=$unit_t_goods_spec;
		$data['pn']=$pn;
        
		$data['coin_code']=$coin_info[0];
        $data['currency']=$coin_info[1];
		$data['goods_cate']=$goods_cate;
		$data['net_weight']=$net_weight;
		$data['gross_weight']=$gross_weight;
        $massage=session('company');
        $data['operator']=$massage['username'];
        $result=$this->product->where("id='".$id."'")->save($data);
		// echo $this->product->getlastsql();
        if($result!==false){
			$warehouse_code=$this->product->where("id='".$id."'")->getField('warehouse_code');
			$warehouse_id=M('Warehouse')->where(array('warehouse_code'=>$warehouse_code))->getField('id');
			// echo $this->product->getlastsql();
			// echo M('Warehouse')->getlastsql();
			$this->push($id,$warehouse_id,1);
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
	public function edit_two($id){
        if($id==''){
            echo "错误";
        }
        $this->result=$this->product->find($id);
		$unit=M('Unit');
		$country=M('Country');
		$coin=M('Coin');
		$book = M('Book',C('DB_PREFIX_T'),C('db_config'));
		
		$book_list=$book->where(array('examined'=>2))->select();
		$unit_list=$unit->select();
		$coin_list=$coin->select();
		$country_list=$country->select();
		
		$this->assign('coin_list',$coin_list);
		$this->assign('book_list',$book_list);
		$this->assign('unit_list',$unit_list);
		$this->assign('country_list',$country_list);
        $this->display();
    }
    public function del($id){
        $g=session('company');
        $db_name=$g['db'];
        $this->p = M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        if($id==''){
            alert(0,'操作有误');
        }
        ///////$id=1@2@3
        $group_id=explode('@',$id);
        $where['product_id']=array('IN',$group_id);
        $count=$this->p->where($where)->count();
        if($count>0){
            alert(0,'部分产品已经制单，不能删除');
        }
        $id=implode(',',$group_id);
        $result=$this->product->delete($id);
        if($result){
            alert(1,'成功删除了'.$result.'条数据，共'.count($group_id).'条');
        }else{
            alert(0,'删除失败');
        }
    }
    public function hscode(){
        $hscode = M('hscode');
        if(I("get.name")!==null && I("get.name")!==''){
            $name_code=trim(I("get.name"));
            if(is_numeric($name_code) && strlen($name_code)>5){
                $where['hscode']=array('like',"%".$name_code."%");
            }else{
                $where['name']=array('like',"%".$name_code."%");
                if($hscode->where($where)->count()==0){//如果么有搜索结果，则分词
                    //将多个空格换成一个空格
                    $name_code=preg_replace("/\s(?=\s)/","\\1",$name_code);
                    $group_word=explode(' ',$name_code);
                    if(count($group_word)==1){
                        $group_word=explode(',',$name_code);
                    }
                    if(count($group_word)==1){
                        $group_word=explode('，',$name_code);
                    }
                    $g_count=count($group_word);
                    $n_where=array();
                    for($i=0;$i++;$i<$g_count){
                        $w=array();$where=array();
                        for($j=0;$j<$i;$j++){
                            $w[]=array('like',"%".$group_word[$j]."%");
                        }
                        $where['name']=$w;
                        if($hscode->where($where)->count()==0){
                            break;
                        }else{
                            $n_where=$where;
                            unset($where,$w);
                        }
                    }
                    if(count($n_where)==0){
                        $where="1=2";//没有记录
                    }else{
                        $where=$n_where;
                    }
                }
            }
        }else{
            $where="1=1";
        }
        $count      = $hscode->where($where)->count();
        $Page       = new \Think\Page($count,15);
        $show       = $Page->show();
        $list = $hscode->where($where)->limit($Page->firstRow.','.$Page->listRows)->order("id")->select();
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->assign('count',$count);
        $this->display();
    }
}